package com.kuzufang.modules.publish;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import com.kuzufang.R;
import com.kuzufang.base.BaseActivity;
import com.kuzufang.modules.main.roommate.RoomConfigurationAdapter;
import com.kuzufang.modules.search.RoommateConfigAdapter;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by songyuanjin on 2017/10/10.
 */

public class PublishFindRoommateActivity extends BaseActivity {
    public static final int REQUEST_CODE = 2001;
    private RecyclerView mRoommateConfigRecyclerView;
    private RoommateConfigAdapter roommateConfigAdapter;

    private RecyclerView mRoomConfigRecyclerView;
    private RoomConfigurationAdapter roomConfigAdapter;

    private RecyclerView mImageRecyclerView;
    private RoomImageAdapter roomImageAdapter;

    private Toolbar mToolbar;
    private TextView tvDate;

    private Calendar calendar;
    private int year, month, day;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_publish_find_roommate);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mRoommateConfigRecyclerView = (RecyclerView) findViewById(R.id.roommate_config);
        roommateConfigAdapter = new RoommateConfigAdapter(this);
        mRoommateConfigRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRoommateConfigRecyclerView.setAdapter(roommateConfigAdapter);
        mRoommateConfigRecyclerView.setFocusable(false);

        mRoomConfigRecyclerView = (RecyclerView) findViewById(R.id.room_recycler_view);
        roomConfigAdapter = new RoomConfigurationAdapter(this);
        mRoomConfigRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRoomConfigRecyclerView.setAdapter(roomConfigAdapter);
        mRoomConfigRecyclerView.setFocusable(false);

        mImageRecyclerView = (RecyclerView) findViewById(R.id.room_recycler_image);
        mImageRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mImageRecyclerView.setFocusable(false);
        roomImageAdapter = new RoomImageAdapter(this);
        mImageRecyclerView.setAdapter(roomImageAdapter);

        tvDate = (TextView) findViewById(R.id.tv_date);
        tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar = Calendar.getInstance();
                year = calendar.get(Calendar.YEAR);       //获取年月日时分秒
                month = calendar.get(Calendar.MONTH);   //获取到的月份是从0开始计数
                day = calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(PublishFindRoommateActivity.this, 0, listener, year, month, day);
                datePickerDialog.show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && data != null) {
            //获取选择器返回的数据
            ArrayList<String> images = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT);
            roomImageAdapter.setImages(images);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.publish_menu, menu);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final View v = PublishFindRoommateActivity.this.findViewById(R.id.publish_confirm);
                if (v != null) {
                    v.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            return false;
                        }
                    });
                }
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.publish_confirm:
                Toast.makeText(this, "Publish", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                onBackPressed();
        }
        return true;
    }

    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int month, int day) {
            tvDate.setText(year + "-" + (++month) + "-" + day);      //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1
        }
    };
}
